package action;

import java.io.PrintWriter;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.infohazard.maverick.ctl.ThrowawayBean2;
//import org.infohazard.maverick.*;

import dao.*;
import entity.*;

public class RegionSVGAction extends ThrowawayBean2 {
	
	private String sheng;
	private String shi;
	
	public void setSheng(String sheng) {
		this.sheng = sheng;
	}
	public String getSheng() {
		return sheng;
	}
	
	public void setShi(String shi) {
		this.shi = shi;
	}
	public String getShi() {
		return shi;
	}
	
	@Override
	protected String perform() throws Exception {

		HttpServletRequest request = this.getCtx().getRequest();
		request.setCharacterEncoding("UTF-8");
		HttpServletResponse response = this.getCtx().getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.write("<svg id='SvgElement' viewbox='0 0 900 520'>");
		
		System.out.println("enter action");

		List<Sheng> guo;
		List<Shi> shengshi;
		List<Shixian> shengxian;
		List<Shixian> shixian;

		int i;
		DataOperationDao dataDao = new DataOperationDao();
		if(sheng.equals("none")&&shi.equals("none"))
			;
		else if(sheng.equals("guo")){
			guo = dataDao.regionGuo();
			for(i=0;i<guo.size();i++){
				writer.write("<path id='"+guo.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
						"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
						"name='"+guo.get(i).getName()+"' shengCode='"+guo.get(i).getShengCode()+"' d='"+guo.get(i).getPath()+
						"' cx='"+guo.get(i).getCx()+"' cy='"+guo.get(i).getCy()+"' />");
			}
		}
		else if(shi.equals("shi")){
			shengshi = dataDao.regionShengshi(sheng);
			for(i=0;i<shengshi.size();i++){
				if(shengshi.get(i).getId()!=8888){
					writer.write("<path id='"+shengshi.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
							"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
							"name='"+shengshi.get(i).getName()+"' shengCode='"+shengshi.get(i).getShengCode()+
							"' shiCode='"+shengshi.get(i).getShiCode()+"' d='"+shengshi.get(i).getPath()+
							"' cx='"+shengshi.get(i).getCx()+"' cy='"+shengshi.get(i).getCy()+"' />");
				}
				else{
					writer.write("<path id='"+shengshi.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
							"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
							"name='"+shengshi.get(i).getName()+"' shengCode='"+shengshi.get(i).getShengCode()+
							"' shiCode='"+shengshi.get(i).getShiCode()+"' d='"+shengshi.get(i).getPath()+
							"' cx='"+shengshi.get(i).getCx()+"' cy='"+shengshi.get(i).getCy()+
							"' minx='"+shengshi.get(i).getMinx()+"' miny='"+shengshi.get(i).getMiny()+
							"' maxx='"+shengshi.get(i).getMaxx()+"' maxy='"+shengshi.get(i).getMaxy()+"' />");
				}
			}
		}
		else if(shi.equals("xian")){
			shengxian = dataDao.regionShengxian(sheng);
			for(i=0;i<shengxian.size();i++){
				if(shengxian.get(i).getId()!=8888){
					writer.write("<path id='"+shengxian.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
							"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
							"name='"+shengxian.get(i).getName()+"' shengCode='"+shengxian.get(i).getShengCode()+
							"' shiCode='"+shengxian.get(i).getShiCode()+"' xianCode='"+shengxian.get(i).getXianCode()+"' d='"+shengxian.get(i).getPath()+
							"' cx='"+shengxian.get(i).getCx()+"' cy='"+shengxian.get(i).getCy()+"' />");
				}
				else{
					writer.write("<path id='"+shengxian.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
							"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
							"name='"+shengxian.get(i).getName()+"' shengCode='"+shengxian.get(i).getShengCode()+
							"' shiCode='"+shengxian.get(i).getShiCode()+"' xianCode='"+shengxian.get(i).getXianCode()+"' d='"+shengxian.get(i).getPath()+
							"' cx='"+shengxian.get(i).getCx()+"' cy='"+shengxian.get(i).getCy()+
							"' minx='"+shengxian.get(i).getMinx()+"' miny='"+shengxian.get(i).getMiny()+
							"' maxx='"+shengxian.get(i).getMaxx()+"' maxy='"+shengxian.get(i).getMaxy()+"' />");
				}
			}
		}
		else{
			shixian = dataDao.regionShixian(sheng,shi);
			for(i=0;i<shixian.size();i++){
				if(shixian.get(i).getId()!=8888){
					writer.write("<path id='"+shixian.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
							"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
							"name='"+shixian.get(i).getName()+"' shengCode='"+shixian.get(i).getShengCode()+
							"' shiCode='"+shixian.get(i).getShiCode()+"' xianCode='"+shixian.get(i).getXianCode()+"' d='"+shixian.get(i).getPath()+
							"' cx='"+shixian.get(i).getCx()+"' cy='"+shixian.get(i).getCy()+"' />");
				}
				else{
					writer.write("<path id='"+shixian.get(i).getId()+"' fill='none' stroke='black' stroke-width='0.1' " +
							"stroke-linecap='round' stroke-linejoin='round' onmouseover='svgmouseover(this)' onmouseout='svgmouseout(this)' "+
							"name='"+shixian.get(i).getName()+"' shengCode='"+shixian.get(i).getShengCode()+
							"' shiCode='"+shixian.get(i).getShiCode()+"' xianCode='"+shixian.get(i).getXianCode()+"' d='"+shixian.get(i).getPath()+
							"' cx='"+shixian.get(i).getCx()+"' cy='"+shixian.get(i).getCy()+
							"' minx='"+shixian.get(i).getMinx()+"' miny='"+shixian.get(i).getMiny()+
							"' maxx='"+shixian.get(i).getMaxx()+"' maxy='"+shixian.get(i).getMaxy()+"' />");
				}
			}
		}

		writer.write("</svg>");
		System.out.println("leave action");
		writer.close();

		return SUCCESS;
	}
}
